﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.IO;
using System.Web;
using System.Web.Services;
using System.Web.UI.WebControls;

namespace wdss.WebServices
{
    /// <summary>
    /// Summary description for CeoService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    //[ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class CeoService : System.Web.Services.WebService
    {

        public CeoService()
        { }
        public void shareOnUpload(int userid, string fileName, FileUpload FileUploadPath)
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["aspnetdbConnectionString"].ConnectionString);
            SqlCommand cmd = new SqlCommand("select dept_id from User1_Dept where user1_id =" + userid, con);
            con.Open();
            int deptID = (int)cmd.ExecuteScalar();
            con.Close();
            SqlCommand Command = new SqlCommand("sp_GetCEOID", con);
            Command.CommandType = CommandType.StoredProcedure;
            Command.Parameters.Add(new SqlParameter("@par_deptid", SqlDbType.Int)).Value = deptID;
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter();
            try
            {
                con.Open();
                //Command.ExecuteNonQuery();
                da.SelectCommand = Command;
                da.Fill(dt);
                con.Close();
            }
            catch (Exception ex)
            { }
            String fileExtension = System.IO.Path.GetExtension(FileUploadPath.FileName).ToLower();
            SqlConnection con1 = new SqlConnection(ConfigurationManager.ConnectionStrings["aspnetdbConnectionString"].ConnectionString);
            cmd = new SqlCommand("select doc_id from Document where doc_title='" + fileName + fileExtension + "'", con1);
            con1.Open();
            int docid = (int)cmd.ExecuteScalar();
            con1.Close();
            ShareDocumentService sharewithCEO = new ShareDocumentService();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                String ceoName = (String)dt.Rows[i]["user1_name"];
                sharewithCEO.ShareDocument(ceoName, userid, docid, true, true, true);
            }
        }
        public void newCEO(int userid, string user_name)
        {

            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["aspnetdbConnectionString"].ConnectionString);
            SqlCommand cmd = new SqlCommand("select dept_id from User1_Dept where user1_id =" + userid, con);
            con.Open();
            int deptID = (int)cmd.ExecuteScalar();
            con.Close();
            SqlConnection con1 = new SqlConnection(ConfigurationManager.ConnectionStrings["aspnetdbConnectionString"].ConnectionString);
            cmd = new SqlCommand("sp_GetExistingDocID",con1);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@par_deptID", SqlDbType.Int)).Value = deptID;
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter();
            try
            {
                con1.Open();
                //Command.ExecuteNonQuery();
                da.SelectCommand = cmd;
                da.Fill(dt);
                con1.Close();
            }
            catch (Exception ex)
            { }
            ShareDocumentService sharewithCEO = new ShareDocumentService();
            try
            {
                con1.Open();
                int docid;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    docid = (int)dt.Rows[i]["doc_id"];
                    cmd = new SqlCommand("select user1_id from Document where doc_id = " + docid, con1);
                    int source = (int)cmd.ExecuteScalar();
                    sharewithCEO.ShareDocument(user_name, source, docid, true, true, true);
                }
                con1.Close();
            }
            catch (Exception ex)
            { }
        }
    }
}
